package com.atung.merchant.service.impl;

import com.atung.merchant.config.auth.AuthUserMangerContext;
import com.atung.merchant.enums.OrderState;
import com.atung.merchant.enums.RoomState;
import com.atung.merchant.enums.RoomType;
import com.atung.merchant.mapper.BusMerchantOrderMapper;
import com.atung.merchant.mapper.BusMerchantRoomMapper;
import com.atung.merchant.po.BusMerchantOrder;
import com.atung.merchant.po.BusMerchantRoom;
import com.atung.merchant.result.ApiResult;
import com.atung.merchant.service.RoomService;
import com.atung.merchant.vo.IdRequest;
import com.atung.merchant.vo.RoomInfoEntity;
import com.atung.merchant.vo.query.QueryRoomInfoRequest;
import com.atung.merchant.vo.save.SaveRoomRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@Service
public class RoomServiceImpl implements RoomService {
    @Resource
    private BusMerchantRoomMapper busMerchantRoomMapper;
    @Resource
    private BusMerchantOrderMapper busMerchantOrderMapper;

    @Override
    public Integer updateRoomStateById(Long id, RoomState roomState) {
        BusMerchantRoom busMerchantRoom = busMerchantRoomMapper.selectById(id);
        busMerchantRoom.setRoomState(roomState.ordinal());
        return busMerchantRoomMapper.updateById(busMerchantRoom);
    }

    @Override
    public BusMerchantRoom selectRoomById(Long id) {
        return busMerchantRoomMapper.selectById(id);
    }

    @Override
    public ApiResult saveRoomInfoService(SaveRoomRequest saveRoomRequest) {
        if (Objects.nonNull(saveRoomRequest.getId())) {
            BusMerchantRoom busMerchantRoom = busMerchantRoomMapper.selectById(saveRoomRequest.getId());
            if (Objects.isNull(busMerchantRoom)) {

                return ApiResult.fail("房间修改失败");
            }
            busMerchantRoom.setName(saveRoomRequest.getName());
            busMerchantRoom.setCapacity(saveRoomRequest.getCapacity());
            busMerchantRoom.setLaber(saveRoomRequest.getLaber());
            busMerchantRoomMapper.updateById(busMerchantRoom);
        }
        BusMerchantRoom busMerchantRoom = new BusMerchantRoom();
        BeanUtils.copyProperties(saveRoomRequest, busMerchantRoom);
        busMerchantRoom.setMerchantId(AuthUserMangerContext.getMerchantId());
        busMerchantRoomMapper.insert(busMerchantRoom);
        return ApiResult.ok();
    }

    @Override
    public List<RoomInfoEntity> getRoomListService(QueryRoomInfoRequest queryRoomInfoRequest) {
        QueryWrapper<BusMerchantRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", AuthUserMangerContext.getMerchantId());
        if(Objects.nonNull(queryRoomInfoRequest.getRoomState())){
            queryWrapper.eq("room_state", queryRoomInfoRequest.getRoomState().ordinal());
        }
        queryWrapper.orderByAsc("room_state");
        queryWrapper.orderByDesc("creator_time");
        List<BusMerchantRoom> busMerchantRooms = busMerchantRoomMapper.selectList(queryWrapper);
        Map<Long, BusMerchantOrder> busMerchantMap = new HashMap<>();
        if (RoomState.useRoom.equals(queryRoomInfoRequest.getRoomState())) {
            QueryWrapper<BusMerchantOrder> queryMerchantOrder = new QueryWrapper<>();
            queryMerchantOrder.eq("merchant_id", AuthUserMangerContext.getMerchantId());
            queryMerchantOrder.eq("state", OrderState.AWAIT.ordinal());
            queryMerchantOrder.eq("room_type", RoomType.ROOM.name());
            busMerchantMap.putAll(busMerchantOrderMapper.selectList(queryMerchantOrder).stream()
                    .collect(Collectors.toMap(
                            BusMerchantOrder::getRoomId, // 假设每个订单都有一个唯一的ID作为键
                            Function.identity(),   // 订单本身作为值
                            (existing, replacement) -> existing // 在键冲突时保留现有的元素
                    )));

        }
        return busMerchantRooms.stream().map(item -> {
            //如果是使用
            if (item.getRoomState().equals(RoomState.useRoom.ordinal())) {
                if (busMerchantMap.containsKey(item.getId())) {
                    BusMerchantOrder busMerchantOrder = busMerchantMap.get(item.getId());
                    return RoomInfoEntity.builder()
                            .id(item.getId())
                            .name(item.getName())
                            .roomState(item.getRoomState())
                            .capacity(item.getCapacity())
                            .orderId(busMerchantOrder.getId())
                            .useTime(busMerchantOrder.getCreatorTime())
                            .build();
                }
            }
            return RoomInfoEntity.builder().id(item.getId()).name(item.getName())
                    .roomState(item.getRoomState())
                    .capacity(item.getCapacity())
                    .build();
        }).collect(Collectors.toList());
    }

    @Override
    public ApiResult deleteRoomInfoService(IdRequest idRequest) {
        BusMerchantRoom busMerchantRoom = busMerchantRoomMapper.selectById(idRequest.getId());
        if (Objects.isNull(busMerchantRoom) || !AuthUserMangerContext.getMerchantId().equals(busMerchantRoom.getMerchantId())) {
            return ApiResult.fail("房间删除失败");
        }
        busMerchantRoomMapper.deleteById(idRequest.getId());
        return ApiResult.ok();
    }

}
